package com.fingard.dsp.bank.directbank.xib01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;

import org.apache.axis.utils.StringUtils;

/**
 * Created by F on 2017/7/18.
 */
public class XIB01SinglePay extends XIB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        return p_req1908.singleDetail.sameBnk.equals("1") ? payBetweenSameBanks(p_req1908) : payBetweenDifferentBanks(p_req1908);
    }

    /**
     * 同行转账
     *
     * @param p_req1908 请求参数
     * @return 单笔支付响应结果
     */
    protected RetAtsSingle payBetweenSameBanks(ReqSingle p_req1908) throws Exception {
        RetAtsSingle Resp = new RetAtsSingle(reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String txnCode = "IESI109005";

        String transId = p_req1908.reqHeader.reqSeqID;
        //账号
        String accountNo = p_req1908.singleDetail.payAct;
        //付款户名称
        String debitAcctName = p_req1908.singleDetail.payName;
        //收款账号
        String creditAcctNo = p_req1908.singleDetail.recAct;
        //收款户姓名
        String creditAcctName = p_req1908.singleDetail.recName;
        //金额
        String amount = p_req1908.singleDetail.payAmount;
        //币种
        String currency = "CNY";
        //用途
        String paymentUsage = p_req1908.singleDetail.purpose;
        if (StringUtils.isEmpty(paymentUsage)) {
            paymentUsage = "未知";
        }
        //附言
        String addtlInf = p_req1908.singleDetail.memo;

        String clientReq = writeHeader(txnCode);

        clientReq += "{\"transId\":\"" + transId + "\",";
        clientReq += "\"accountNo\":\"" + accountNo + "\",";
        clientReq += "\"debitAcctName\":\"" + debitAcctName + "\",";
        clientReq += "\"creditAcctNo\":\"" + creditAcctNo + "\",";
        clientReq += "\"creditAcctName\":\"" + creditAcctName + "\",";
        clientReq += "\"amount\":\"" + amount + "\",";
        clientReq += "\"currency\":\"" + currency + "\",";
        clientReq += "\"paymentUsage\":\"" + paymentUsage + "\",";
        clientReq += "\"addtlInf\":\"" + addtlInf + "\"}";
        clientReq += "}";

        String sign = Signatures(clientReq);
        String bankRet = postData(sign, bankFront.serverIP, bankFront.serverPort);

        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode, respMsg)) {
            Resp.writeStart();
            Resp.writeRespCode("-1");
            Resp.writeRespInfo(respMsg);
            Resp.writeEnd();
            return Resp;
        }

        JSONObject body = bankResp.getJSONObject("body");
        detail.respCode = "0";
        detail.respInfo = respMsg;
        detail.transState = retCode + "+" + body.getString("transStatus");
        detail.payInfoCode = retCode + "+" + body.getString("transStatus");
        detail.reqSeqID = p_req1908.reqHeader.reqSeqID;

        Resp.writeRetDetail(detail);
        Resp.writeEnd();
        return Resp;
    }

    /**
     * 跨行转账
     *
     * @param p_req1908 请求参数
     * @return 单笔支付响应结果
     */
    protected RetAtsSingle payBetweenDifferentBanks(ReqSingle p_req1908) throws Exception {
        RetAtsSingle Resp = new RetAtsSingle(reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String txnCode = "IESI109006";


        String transId = p_req1908.reqHeader.reqSeqID;
        //账号
        String accountNo = p_req1908.singleDetail.payAct;
        //付款户名称
        String debitAcctName = p_req1908.singleDetail.payName;
        //收款账号
        String creditAcctNo = p_req1908.singleDetail.recAct;
        //收款户姓名
        String creditAcctName = p_req1908.singleDetail.recName;
        //收款行行号(联行号)
        String receiveBankNo = p_req1908.singleDetail.cnapsCode;
        //收款行名称
        String receiveBankName = p_req1908.singleDetail.cnapsName;
        //金额
        String amount = p_req1908.singleDetail.payAmount;
        //币种
        String currency = "CNY";
        //用途
        String paymentUsage = p_req1908.singleDetail.purpose;
        if (StringUtils.isEmpty(paymentUsage)) {
            paymentUsage = "未知";
        }
        //附言
        String addtlInf = p_req1908.singleDetail.memo;
        //交易类型
        String transType = "1";

        String clientReq = writeHeader(txnCode);

        clientReq += "{\"transId\":\"" + transId + "\",";
        clientReq += "\"accountNo\":\"" + accountNo + "\",";
        clientReq += "\"debitAcctName\":\"" + debitAcctName + "\",";
        clientReq += "\"creditAcctNo\":\"" + creditAcctNo + "\",";
        clientReq += "\"creditAcctName\":\"" + creditAcctName + "\",";
        clientReq += "\"receiveBankNo\":\"" + receiveBankNo + "\",";
        clientReq += "\"receiveBankName\":\"" + receiveBankName + "\",";
        clientReq += "\"amount\":\"" + amount + "\",";
        clientReq += "\"currency\":\"" + currency + "\",";
        clientReq += "\"paymentUsage\":\"" + paymentUsage + "\",";
        clientReq += "\"addtlInf\":\"" + addtlInf + "\",";
        clientReq += "\"transType\":\"" + transType + "\"}";
        clientReq += "}";
        String sign = Signatures(clientReq);
        String bankRet = postData(sign, bankFront.serverIP, bankFront.serverPort);

        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode, respMsg)) {
            Resp.writeStart();
            Resp.writeRespCode("-1");
            Resp.writeRespInfo(respMsg);
            Resp.writeEnd();
            return Resp;
        }

        JSONObject body = bankResp.getJSONObject("body");
        detail.respCode = "0";
        detail.respInfo = respMsg;
        detail.transState = retCode + "+" + body.getString("transStatus");
        detail.payInfoCode = retCode + "+" + body.getString("transStatus");
        detail.reqSeqID = p_req1908.reqHeader.reqSeqID;
        Resp.writeRetDetail(detail);
        Resp.writeEnd();
        return Resp;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        String txnCode = "IESI109008";
        RetAtsSingle Resp = new RetAtsSingle(reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String transId = p_req1944.reqHeader.reqSeqID;
        String clientReq = writeHeader(txnCode);
        clientReq += "{\"transId\":\"" + transId + "\"}";
        clientReq += "}";

        String signdata = Signatures(clientReq);
        String bankRet = postData(signdata, bankFront.serverIP, bankFront.serverPort);

        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode, respMsg)) {
            Resp.writeStart();
            Resp.writeRespCode("-1");
            Resp.writeRespInfo(respMsg);
            Resp.writeEnd();
            return Resp;
        }

        JSONObject body = bankResp.getJSONObject("body");
        detail.respCode = "0";
        detail.respInfo = respMsg;
        detail.transState = retCode + "+" + body.getString("transStatus");
        detail.payInfoCode = retCode + "+" + body.getString("transStatus");
        detail.reqSeqID = p_req1944.reqHeader.reqSeqID;
        Resp.writeRetDetail(detail);
        Resp.writeEnd();
        return Resp;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
